﻿/*
 * This file is part of CommunitySettlers.
 *
 * CommunitySettlers is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.

 * CommunitySettlers is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with CommunitySettlers.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * Author: Christoph Husse
 * 
 * Also checkout our homepage: http://communitysettlers.codeplex.com/
 */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SettlersEngine
{
    internal static class Statistics
    {
        internal static Double PercentDiff(Double inA, Double inB)
        {
            Double diff = Math.Abs(inA - inB);
            Double max = Math.Max(Math.Abs(inA), Math.Abs(inB));

            return diff / max;
        }

        internal static double MeanArithmetic(this IEnumerable<double> source)
        {
            return source.Average();
        }

        internal static double MeanArithmetic<TSource>(this IEnumerable<TSource> source, Func<TSource, double> selector)
        {
            return source.Average(selector);
        }

        internal static double MeanGeometric(this IEnumerable<double> source)
        {
            return source.MeanGeometric(e => e);
        }

        internal static double MeanGeometric<TSource>(this IEnumerable<TSource> source, Func<TSource, double> selector)
        {
            Int32 count = 0;
            double result = source.Product(e => { 
                Double val = selector(e); 
                
                if (val != 0) { 
                    count++;

                    return val; 
                } else
                    return 1; 
            });

            if(count > 0)
                return Math.Pow(result, 1.0 / count);
            return
                0;
        }

        internal static double MeanHarmonic(this IEnumerable<double> source)
        {
            return source.MeanHarmonic(e => e);
        }

        internal static double MeanHarmonic<TSource>(this IEnumerable<TSource> source, Func<TSource, double> selector)
        {
            Int32 count = 0;
            double result = source.Sum(e =>
            {
                Double val = selector(e);

                if (val != 0)
                {
                    count++;

                    return 1.0 / val;
                }
                else
                    return 1;
            }); 

            if (count > 0)
                return count / result;
            return
                0;
        }


        internal static double Product(this IEnumerable<double> source)
        {
            return source.Product(e => e);
        }

        internal static double ProductNullToOne(this IEnumerable<double> source)
        {
            return source.ProductNullToOne(e => e);
        }

        internal static double Product<TSource>(this IEnumerable<TSource> source, Func<TSource, double> selector)
        {
            IEnumerator<TSource> iter = source.GetEnumerator();

            if (!iter.MoveNext())
                return 0;

            Double result = selector(iter.Current);

            while (iter.MoveNext())
            {
                Double e = selector(iter.Current);

                result *= e;
            }

            return result;
        }

        internal static double ProductNullToOne<TSource>(this IEnumerable<TSource> source, Func<TSource, double> selector)
        {
            IEnumerator<TSource> iter = source.GetEnumerator();

            if (!iter.MoveNext())
                return 0;

            Double result = selector(iter.Current);

            while(iter.MoveNext())
            {
                Double e = selector(iter.Current);

                if(e != 0)
                    result *= e;
            }

            return result;
        }
    }
}
